package com.api;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;

import org.apache.http.util.ByteArrayBuffer;
import org.java_websocket.drafts.Draft_75;

import java.io.IOException;
import java.net.InetAddress;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class LTLink {
    public static String TAG = "LTLinkSSS";
    public static final String VERSION = "V2.1";
    public static LTLink q;
    public static /* synthetic */ int[] t;
    public int b;
    public InetAddress c;
    public List d;
    public List e;
    public int f;
    public int g;
    public int h;
    public String i;
    public byte[] j;
    public byte[] k;
    public byte[] l;
    public boolean m;
    public UDPClient n;
    public b o;
    public c p;
    public int r;
    public Lock s;

    public interface Callback {
        void onFinish();
    }

    public LTLink() {
        this.b = 10;
        this.f = 28888;
        this.o = null;
        this.p = null;
        this.r = 0;
        this.i = new String("lock");
        this.g = this.b;
        this.h = 100;
        this.s = new ReentrantLock();
        this.n = new UDPClient((byte)f);
        this.p = new c(this);
        this.d = new ArrayList();
        this.e = new ArrayList();
    }

    public List a(List list, Boolean bool) {
        int i2;
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();
        int size = list.size();
        int i3 = (size % 8 > 0 ? 1 : 0) + (size / 8);
        arrayList2.add(Integer.valueOf((i3 + 1) | 64));
        arrayList4.add(Byte.valueOf((byte) ((i3 + 1) & 63)));
        int i4 = 0;
        if (bool.booleanValue()) {
            i4 = 1;
        }
        if (this.l != null && this.l.length > 0) {
            i4 |= 2;
        }
        if (this.k != null && this.k.length > 0) {
            i4 |= 4;
        }
        arrayList2.add(Integer.valueOf(i4 | 128));
        arrayList4.add(Byte.valueOf((byte) (i4 & 63)));
        arrayList2.add(Integer.valueOf(this.j.length | 192));
        arrayList4.add(Byte.valueOf((byte) (this.j.length & 63)));
        if (this.k != null && this.k.length > 0) {
            arrayList2.add(Integer.valueOf(this.k.length | 256));
            arrayList4.add(Byte.valueOf((byte) (this.k.length & 63)));
        }
        int size2 = arrayList2.size();
        arrayList2.add(767);
        arrayList2.add(768);
        int i5 = 0;
        int i6 = 528;
        while (size > 0) {
            if (size <= 8) {
                i2 = size;
                size = 0;
            } else {
                i2 = 8;
                size -= 8;
            }
            int i7 = ((Integer) list.get(i5)).intValue() == 0 ? (i2 - 2) | i6 : (i2 - 1) | i6;
            for (int i8 = 0; i8 < 2; i8++) {
                arrayList.add(Integer.valueOf(i7));
            }
            for (int i9 = 0; i9 < i2; i9++) {
                int intValue = ((Integer) list.get(i5)).intValue();
                i5++;
                if (i9 != 0 || intValue != 0) {
                    arrayList.add(Integer.valueOf((i9 << 6) | intValue));
                    arrayList4.add(Byte.valueOf((byte) (intValue & 63)));
                }
            }
            arrayList.add(767);
            arrayList.add(768);
            i6 += 16;
        }
        byte[] bArr = new byte[arrayList4.size()];
        for (int i10 = 0; i10 < arrayList4.size(); i10++) {
            bArr[i10] = ((Byte) arrayList4.get(i10)).byteValue();
        }
//        (byte) (aTest.a(bArr) & 63);
        byte a = (byte) (aTest.a(bArr) & 63);
        if (a != 0) {
            arrayList3.add(Integer.valueOf(size2 | 512));
            arrayList3.add(Integer.valueOf(a & Draft_75.END_OF_FRAME));
        } else {
            arrayList3.add(Integer.valueOf(size2 | 512 | 8));
        }
        arrayList3.addAll(arrayList2);
        arrayList3.addAll(arrayList);
        return arrayList3;
    }

    public static List a(byte[] bArr, Boolean bool) {
        int i2 = 0;
        ArrayList arrayList = new ArrayList();
        if (bool.booleanValue()) {
            while (i2 < bArr.length) {
                arrayList.add(Integer.valueOf(bArr[i2] & Draft_75.END_OF_FRAME));
                i2++;
            }
        } else {
            int i3 = 0;
            for (byte b2 : bArr) {
                i3 = (i3 << 8) | b2;
                i2 += 8;
                while (i2 >= 6) {
                    arrayList.add(Integer.valueOf(i3 >> (i2 - 6)));
                    i2 -= 6;
                    i3 &= ((-1 >> i2) << i2) ^ -1;
                }
            }
            if (i2 > 0) {
                arrayList.add(Integer.valueOf(i3 << (6 - i2)));
            }
        }
        return arrayList;
    }

    public void a(int i2) {
        for (int i3 = 0; i3 < i2 && !this.m; i3++) {
            for (int i4 = 0; i4 < this.e.size() && !this.m; i4++) {
                b(((Integer) this.e.get(i4)).intValue());
            }
        }
    }

    static /* synthetic */ void a(LTLink lTLink, Context context) {
        InetAddress byAddress;
        try {
            DhcpInfo dhcpInfo = ((WifiManager) context.getSystemService("wifi")).getDhcpInfo();
            if (dhcpInfo == null) {
                byAddress = null;
            } else {
                int i2 = (dhcpInfo.ipAddress & dhcpInfo.netmask) | (dhcpInfo.netmask ^ -1);
                byte[] bArr = new byte[4];
                for (int i3 = 0; i3 < 4; i3++) {
                    bArr[i3] = (byte) ((i2 >> (i3 * 8)) & 255);
                }
                byAddress = InetAddress.getByAddress(bArr);
            }
            lTLink.c = byAddress;
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    static /* synthetic */ byte[] a(String str, String str2) {
        if (str2 == null || str2.length() != 16) {
            Log.e(TAG, "Key is not 16 bytes.");
            return null;
        }
        byte[] bytes = str2.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(bytes, "AES");
        Cipher instance = null;
        byte[] doFinal;
        try {
            try {
                instance = Cipher.getInstance("AES/CBC/PKCS7Padding");
                instance.init(1, secretKeySpec, new IvParameterSpec(bytes));
            } catch (InvalidAlgorithmParameterException invalidAlgorithmParameterException) {
                invalidAlgorithmParameterException.printStackTrace();
            } catch (InvalidKeyException invalidKeyException) {
                invalidKeyException.printStackTrace();
            }
            doFinal = instance.doFinal(str.getBytes());
            String str3 = "";
            for (int i2 = 0; i2 < doFinal.length; i2++) {
                str3 = String.valueOf(String.valueOf(str3) + Integer.toHexString(doFinal[i2] & Draft_75.END_OF_FRAME)) + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
            }
            Log.d(TAG, str3);
            return doFinal;
        } catch (Exception e) {
            return null;

        }
    }

    public static Boolean b(byte[] bArr) {
        for (byte b2 : bArr) {
            if ((b2 & 192) != 0) {
                return false;
            }
        }
        return true;
    }

    public void b(int i2) {
        byte[] bArr = new byte[i2];
        bArr[0] = 48;
        Log.d(TAG, "//" + Integer.toHexString(i2));
        this.n.a(bArr, this.c, this.f);
        c(this.g);
    }

    static /* synthetic */ void b(LTLink lTLink) {
        if (lTLink.s.tryLock()) {
            if (lTLink.r == 1) {
                lTLink.a(1);
            } else {
                lTLink.a(2);
                for (int i2 = 0; i2 < lTLink.d.size() && !lTLink.m; i2++) {
                    lTLink.b(((Integer) lTLink.d.get(i2)).intValue());
                }
            }
            lTLink.s.unlock();
        }
    }

    /* access modifiers changed from: public */
    public static void c(int i2) {
        if (i2 != 0) {
            try {
                Thread.sleep((long) i2);
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
    }

    public static /* synthetic */ int[] c() {
        int[] iArr = t;
        if (iArr == null) {
            iArr = new int[Thread.State.values().length];
            try {
                iArr[Thread.State.BLOCKED.ordinal()] = 1;
            } catch (NoSuchFieldError e2) {
            }
            try {
                iArr[Thread.State.NEW.ordinal()] = 2;
            } catch (NoSuchFieldError e3) {
            }
            try {
                iArr[Thread.State.RUNNABLE.ordinal()] = 3;
            } catch (NoSuchFieldError e4) {
            }
            try {
                iArr[Thread.State.TERMINATED.ordinal()] = 4;
            } catch (NoSuchFieldError e5) {
            }
            try {
                iArr[Thread.State.TIMED_WAITING.ordinal()] = 5;
            } catch (NoSuchFieldError e6) {
            }
            try {
                iArr[Thread.State.WAITING.ordinal()] = 6;
            } catch (NoSuchFieldError e7) {
            }
            t = iArr;
        }
        return iArr;
    }

    static /* synthetic */ void e(LTLink lTLink) {
        lTLink.e.clear();
        lTLink.e.add(767);
        lTLink.e.add(768);
    }

    static /* synthetic */ void f(LTLink lTLink) {
        lTLink.d.clear();
        ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(0);
        if (lTLink.j != null) {
            byteArrayBuffer.append(lTLink.j, 0, lTLink.j.length);
        }
        if (lTLink.k != null && lTLink.k.length > 0) {
            byteArrayBuffer.append(lTLink.k, byteArrayBuffer.length(), lTLink.k.length);
        }
        byte[] buffer = byteArrayBuffer.buffer();
        Boolean b2 = b(buffer);
        lTLink.d.addAll(lTLink.a(a(buffer, b2), b2));
    }

    public static LTLink getInstance() {
        if (q == null) {
            q = new LTLink();
        }
        return q;
    }

    public static String getVersion() {
        return VERSION;
    }

    public void setDelay(int i2, int i3) {
        synchronized (this.i) {
            this.g = i2;
            this.h = i3;
        }
    }

    public void startGuidance(Context context) {
        d dVar = new d(this);
        dVar.mContext = context;
        dVar.start();
    }

    public void startLink(String str, String str2, String str3, Context context) {
        e eVar = new e(this);
        eVar.mContext = context;
        eVar.y = str;
        eVar.x = str2;
        eVar.z = str3;
        eVar.start();
    }

    /* JADX INFO: Can't fix incorrect switch cases order, some code will duplicate */
    public void startRun() {
        if (this.o == null) {
            this.o = new b(this, (byte) 0);
        }
        switch (c()[this.o.getState().ordinal()]) {
            case 2:
                this.m = false;
                this.o.start();
                break;
            case 4:
                this.o = new b(this, (byte) 0);
                this.m = false;
                this.o.start();
                break;
            case 5:
            case 6:
                this.m = false;
                break;
        }
        switch (c()[this.p.getState().ordinal()]) {
            case 2:
                break;
            case 3:
            default:
                return;
            case 4:
                this.p = new c(this);
                break;
        }
        this.p.start();
    }

    public void stopLink() {
        this.m = true;
        this.p.d();
    }
}
